package com.test.deencredit.adapter_deencredit;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.test.deencredit.R;
import com.test.deencredit.bean_deencredit.PermissionInfo_deencredit;

import java.util.ArrayList;


public class PermissionViewAdapter_deencredit extends RecyclerView.Adapter<PermissionViewAdapter_deencredit.PermissionDialogHolder_deencredit> {

    private final ArrayList<PermissionInfo_deencredit> infos_deencredit;

    public PermissionViewAdapter_deencredit(ArrayList<PermissionInfo_deencredit> infos){
        this.infos_deencredit = infos;
    }

    public PermissionInfo_deencredit getInfo(int index){
        return infos_deencredit.get(index);
    }

    @NonNull
    @Override
    public PermissionDialogHolder_deencredit onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new PermissionDialogHolder_deencredit(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_permission_deencredit,parent,false));
    }

    @SuppressLint("InflateParams")
    @Override
    public void onBindViewHolder(@NonNull PermissionDialogHolder_deencredit holder, int position) {

        PermissionInfo_deencredit info = infos_deencredit.get(position);

        if (info.getIconResId_deencredit()!=null) holder.setIcon_deencredit(info.getIconResId_deencredit());
        holder.setTitle_deencredit(info.getTitle_deencredit());
        holder.setText_deencredit(info.getText_deencredit());
        info.setHolder_deencredit(holder);

        if (position>=(infos_deencredit.size()-1)) holder.getNextView_deencredit().setVisibility(View.GONE);
        else holder.getNextView_deencredit().setOnClickListener(v -> info.next_deencredit());

    }

    @Override
    public int getItemCount() {
        return infos_deencredit.size();
    }

    public static class PermissionDialogHolder_deencredit extends RecyclerView.ViewHolder{

        private final ImageView imgIv_deencredit;
        private final TextView titleTv_deencredit;
        private final TextView textTv_deencredit;
        private final View nextView_deencredit;

        @SuppressLint("InflateParams")
        public PermissionDialogHolder_deencredit(View view){

            super(view);

            imgIv_deencredit = view.findViewById(R.id.ip_iv_img);
            titleTv_deencredit = view.findViewById(R.id.ip_tv_title);
            textTv_deencredit = view.findViewById(R.id.ip_tv_text);
            nextView_deencredit = view.findViewById(R.id.ip_view_next);

        }

        public void setIcon_deencredit(int iconresid){
            imgIv_deencredit.setImageResource(iconresid);
        }

        public void setTitle_deencredit(String title) {
            titleTv_deencredit.setText(title);
        }

        public void setText_deencredit(String text){
            textTv_deencredit.setText(text);
        }

        public View getNextView_deencredit() {
            return nextView_deencredit;
        }
    }
}
